package controllers

import (
	"gitee.com/igolang/imoney/internal/app/system/services"
	servicesMonitor "gitee.com/igolang/imoney/internal/app/system/services/monitor"
	"gitee.com/igolang/imoney/internal/common/httpResp"
	"github.com/gin-gonic/gin"
)

var Monitor = &monitor{}

type monitor struct{}

func (c *monitor) OnlineList(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := servicesMonitor.Online.List(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *monitor) OnlineQuit(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	err := services.User.Quit(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *monitor) Server(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := servicesMonitor.Server.Index(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *monitor) JobList(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := servicesMonitor.Job.List(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *monitor) JobLogList(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := servicesMonitor.JobLog.List(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *monitor) JobDetail(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := servicesMonitor.Job.Detail(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *monitor) JobExport(ctx *gin.Context) {
	r := httpResp.Response()
	b, err := servicesMonitor.Job.Export(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		httpResp.JSON(ctx, r)
		return
	}
	httpResp.Export(ctx, b, "xlsx")
}

func (c *monitor) JobLogExport(ctx *gin.Context) {
	r := httpResp.Response()
	b, err := servicesMonitor.JobLog.Export(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		httpResp.JSON(ctx, r)
		return
	}
	httpResp.Export(ctx, b, "xlsx")
}

func (c *monitor) JobDelete(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	err := servicesMonitor.Job.Delete(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *monitor) JobLogDelete(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	err := servicesMonitor.JobLog.Delete(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *monitor) JobLogClean(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	err := servicesMonitor.JobLog.Clean()
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *monitor) JobSave(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	err := servicesMonitor.Job.Save(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *monitor) JobChangeStatus(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	err := servicesMonitor.Job.ChangeStatus(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *monitor) JobRun(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	err := servicesMonitor.Job.Run(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}
